package com.mall.controller.backend;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.Const;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.MmallCategory;
import com.mall.pojo.MmallUser;
import com.mall.service.ICategoryService;
import com.mall.service.IUserService;
import com.mall.util.CookieUtil;
import com.mall.util.JsonUtil;
import com.mall.util.RedisPoolUtil;

@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {
	
	@Autowired
	private ICategoryService categoryService;
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/add_category.do")
	@ResponseBody
	public ServerResponse<String> addCategory(HttpServletRequest request,String categoryName,@RequestParam(value="parentId",defaultValue="0")int parentId){
		
		//检测用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录" );
		}
		
		//检测用户是否是管理员 
		if(userService.checkAdminRole(user).isSuccess()){
			//是管理员,可以增加分类
			return categoryService.addCategory(categoryName,parentId);
		}else{
			
			return ServerResponse.createByErrorMessage("不是管理员,没有权限操作");
		}
	}
	
	@RequestMapping("/set_category_name.do")
	@ResponseBody						//修改品类名称
	public ServerResponse<String> updateCategoey(HttpServletRequest request,String categoryName ,Integer categoryId){
		
		//检查用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
		}
		
		//检查用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			//是管理员 , 可以修改品类名
			return categoryService.updateCategory(categoryName,categoryId);
		}else{
			return ServerResponse.createByErrorMessage("不是管理员无权限操作");
		}
	}
	
	@RequestMapping("/get_category.do")
	@ResponseBody					//1.获取品类子节点(平级)
	public ServerResponse<List<MmallCategory>> getCategory(HttpServletRequest request, @RequestParam(value="categoryId",defaultValue="0")Integer categoryId){
		
		//检查用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
		}
		 
		//检查用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			//是管理员, 执行查询操作,不递归保持平级
			return categoryService.getCategory(categoryId);
		}else{ 
			return ServerResponse.createByErrorMessage("查找品类信息失败");
		}
	}
	
	@RequestMapping("/get_deep_category.do")
	@ResponseBody			//获取当前分类id及递归子节点categoryId
	public ServerResponse<List<Integer>> getDeepCategory(HttpServletRequest request , Integer categoryId){
		
		//校验用户是否登录
		//MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		
		String loginToken = CookieUtil.readLoginToken(request);

		if(StringUtils.isEmpty(loginToken)){
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		
		String userStr = RedisPoolUtil.get(loginToken);
		MmallUser user = JsonUtil.string2Obj(userStr, MmallUser.class);
		
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请先登录");
		}
		
		//校验用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			//是管理员
			return categoryService.getDeepCategory(categoryId);
		}else{
			return ServerResponse.createByErrorMessage("不是管理员,无权限操作");
		}
				
	}
	
	
}	
